function hasChildren(item) {
  return Array.isArray(item.children) && item.children.length > 0;
}
function sortByChildren(arr) {
  let hasChildrenArr = arr.filter((item) => hasChildren(item));
  let noChildrenArr = arr.filter((item) => !hasChildren(item));
  let concatArr = hasChildrenArr.concat(noChildrenArr);

  return concatArr.map((item) => {
    item.children =
      (hasChildren(item) && sortByChildren(item.children)) || [];
    return item;
  });
}

let arr = [{ data: 3 }, { data: 1, children: [{ data: 2, children: [] },{ data: 3, children: [{ data: 2, children: [] }] }] }];

console.log(sortByChildren(arr));

function isPalindrome2(str) {
  let end = str.length;
  console.log(end);
  if (end == 0 || end == 1) return true;
  let start = 0;
  end--;
  while (start < end) {
    if (str[start] !== str[end]) return false;
    start++;
    end--;
  }
  return true;
}
console.log(isPalindrome2(-121));
